package com.woniuxy.cinemaServer.controller;



import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniuxy.cinemaServer.dto.cinema.CinemaInfoDto;
import com.woniuxy.cinemaServer.form.cinemaInfo.CinemaLoadForm;
import com.woniuxy.cinemaServer.form.cinemaInfo.CinemaAddForm;
import com.woniuxy.cinemaServer.form.cinemaInfo.CinemaChangeForm;
import com.woniuxy.cinemaServer.model.CinemaInfo;
import com.woniuxy.cinemaServer.param.CinemaChangeParam;
import com.woniuxy.cinemaServer.param.cinema.CinemaAddParam;
import com.woniuxy.cinemaServer.param.cinema.CinemaLoadParam;
import com.woniuxy.cinemaServer.service.CinemaInfoService;
import com.woniuxy.commons.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.hutool.core.bean.BeanUtil.toBean;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年03月20日
 */
@RestController
@RequestMapping("/cinemaInfo")
@Api(tags = "放映厅相关接口")
public class CinemaInfoController {
    @Autowired
    private CinemaInfoService cinemaInfoService;

    @PostMapping("/add.api")
    @ApiOperation("新增")
    public Result add(@RequestBody CinemaAddForm form){
        CinemaAddParam param = toBean(form, CinemaAddParam.class);
        cinemaInfoService.add(param);
        return Result.getResult().setMsg("新增成功");
    }

    @PostMapping("del/{id}.api")
    @ApiOperation("删除")
    public Result del(@PathVariable("id") Integer id){
        cinemaInfoService.del(id);
        return Result.getResult().setMsg("删除放映厅成功");
    }

    @PostMapping("change.api")
    @ApiOperation("修改")
    public Result change(@RequestBody CinemaChangeForm form){
        CinemaChangeParam param = toBean(form, CinemaChangeParam.class);
        cinemaInfoService.change(param);
        return Result.getResult().setMsg("修改放映厅成功");
    }

    @PostMapping("/load")
    @ApiOperation("查询")
    public Result load(@RequestBody CinemaLoadForm form){
        CinemaLoadParam param = toBean(form, CinemaLoadParam.class);
        PageInfo<CinemaInfoDto> pageInfo=cinemaInfoService.load(param);
        return Result.getResult().setMsg("查询放映厅信息成功").setData(pageInfo);
    }

    @GetMapping("/loadById/{id}.api")
    public Result loadCinemaById(@PathVariable("id") Integer id){

        return Result.getResult().setMsg("根据id查找放映厅成功").setData(cinemaInfoService.getById(id));
    }

    @PostMapping("/loadByIds.api")
    public Result<List<CinemaInfo>> loadCinemaByIds(@RequestBody List<Integer> ids)throws Exception{
        List<CinemaInfo> cinemaInfos = cinemaInfoService.listByIds(ids);
        return Result.getListResult(CinemaInfo.class).setMsg("根据ids查询成功").setData(cinemaInfos);
    }

}

